package org.herui.campus.service;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mlnx.common.entity.BaseEntity;
import com.mlnx.common.utils.MyLog;
import org.herui.campus.service.cache.UserCachService;
import org.shan.spring.base.utils.ServletUtils;
import org.shan.spring.cache.CacheChannel;
import org.shan.spring.cache.CacheObject;
import org.shan.spring.mybatis.service.BaseCurdService;
import org.shan.spring.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * Created by amanda.shan on 2019/7/15.
 */
public class BaseService<M extends BaseMapper<T>, T> extends BaseCurdService<M, T> {

    protected MyLog log = MyLog.getLog(getClass());

    @Autowired
    protected CacheChannel cacheChannel;

    @Autowired
    protected RedisUtil redisUtil;

    @Autowired
    protected UserCachService userCachService;

    public void evict(String region, String key) {
        cacheChannel.evict(region, key);
    }


    public void clear(String region) {
        cacheChannel.clear(region);
    }

    public <W> W getCache(String region, String key, Class<W> tClass) {
        CacheObject cacheObject = cacheChannel.get(region, key);
        return cacheObject.getObject(tClass);
    }

    public Map<String, CacheObject> getCaches(String region, Collection<String> keys) {
        return cacheChannel.get(region, keys);
    }


    public void setCache(String region, String key, Object value) {
        cacheChannel.set(region, key, value);
    }

    public String getUsername() {

        Object username = ServletUtils.getAttribute("username");
        if (username != null) {
            return username.toString();
        } else {
            return "";
        }
    }

    public Integer getUserId(){
        return userCachService.getUserId();
    }

    /**
     * 包装 插入创建者和创建时间
     *
     * @param t
     * @param <T>
     * @return
     */
    public <T extends BaseEntity> T wrapperCreator(T t) {
        String username;
        try {
            username = ServletUtils.getAttribute("username");
        } catch (Exception e) {
            username = "系统";
        }
        if (username != null) {
            t.setCreater(username.toString());
        }
        t.setCreateTime(new Date());

        return t;
    }

    /**
     * 包装 插入创建者和创建时间
     *
     * @param t
     * @param <T>
     * @return
     */
    public <T extends BaseEntity> T wrapperEditor(T t) {
        String username = null;
        try {
            username = ServletUtils.getAttribute("username");
        } catch (Exception e) {
            username = "系统";
        }
        if (username != null) {
            t.setEditor(username.toString());
        }
        t.setEditTime(new Date());

        return t;
    }

    /**
     * 推送消息
     *
     * @param channel 消息的key
     * @param message 消息的内容，可以是一个任意对象， 会将其转化为json数据传输， 收到的永远是string
     */
    public void push(String channel, Object message) {
        redisUtil.push(channel, message);
    }



}
